package com.example.jones.test;

import java.util.Calendar;

import org.json.JSONArray;
import org.w3c.dom.Text;

import com.example.util.HttpUtil;
import com.example.util.NumberUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeView {
    private Context mcontext = null;
    private View mView = null;
    private Handler mHd = null;
    private ListView mr_top = null; // 每日推荐
    private TopAdepter cookbookadapter = null; // 每日推荐
    private HotAdepter hot = null;
    private MatchAdepter sap = null;
    private TextView time_tv = null; // 动态显示食材季节
    private com.example.jones.test.GalleryFlows gf_hot, gf_sc;
    private TextView tv_sg, tv_sc, tv_qt;

    public HomeView(Context c) {
        this.mcontext = c;
    }

    /**
     * 返回布局
     */
    public View getView() {

        mView = View.inflate(mcontext, R.layout.home, null);
        mr_top = (ListView) mView.findViewById(R.id.mr_top);
        gf_hot = (GalleryFlows) mView.findViewById(R.id.gf_hot);
        gf_sc = (GalleryFlows) mView.findViewById(R.id.gf_sc);

        tv_sg = (TextView) mView.findViewById(R.id.tv_sg);
        tv_sc = (TextView) mView.findViewById(R.id.tv_sc);
        tv_qt = (TextView) mView.findViewById(R.id.tv_qt);
        time_tv = (TextView) mView.findViewById(R.id.time_tv);

        System.out.println("####################进入HomeView");
        // 连接网络
        String url = HttpUtil.Base_url + "/DailyRecommendService";

        try {
            // 每日推荐部分
            JSONArray dailyRecommendjsonArray = new JSONArray(
                    HttpUtil.getRequest(url + "?type=0"));
            // 将JSONArray包装成Adapter
            cookbookadapter = new TopAdepter(handler, dailyRecommendjsonArray,
                    mcontext);
            mr_top.setAdapter(cookbookadapter);
            setListViewHeightBasedOnChildren(mr_top);

            // 热门菜谱部分
            JSONArray hotjsonArray = new JSONArray(HttpUtil.getRequest(url
                    + "?type=3"));
            hot = new HotAdepter(handler, hotjsonArray, mcontext);
            gf_hot.setAdapter(hot);
            gf_hot.setSelection(1);
            
//            // shil
//            JSONArray hotjsonArray = new JSONArray(HttpUtil.getRequest(url
//                    + "?type=3"));
//            hot = new HotAdepter(handler, hotjsonArray, mcontext);
//            gf_hot.setAdapter(hot);
//            gf_hot.setSelection(1);

        } catch (Exception e) {
            e.printStackTrace();
        }

        time_tv.setText(NumberUtils.numberArab2CN(Calendar.getInstance().get(
                Calendar.MONTH) + 1)
                + "月时令食材");
        // 三月时令食材部分、
        sap = new MatchAdepter();
        gf_sc.setAdapter(sap);
        gf_sc.setSelection(2);

        // 下面分类设置监听
        tv_sg.setOnClickListener(onclick);
        tv_sc.setOnClickListener(onclick);
        tv_qt.setOnClickListener(onclick);

        // 热门
        gf_hot.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent in = new Intent();
                in.setClass(mcontext, DtlActivity.class);
                mcontext.startActivity(in);
            }
        });

        // 时令
        gf_sc.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent in = new Intent();
                in.setClass(mcontext, DtlActivity.class);
                mcontext.startActivity(in);
            }
        });

        gf_hot.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                if (arg2 == 9) {
                    gf_hot.setSelection(1);
                } else if (arg2 == 0) {

                    gf_hot.setSelection(1);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        gf_sc.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                if (arg2 >= 8) {
                    gf_sc.setSelection(1);
                } else if (arg2 == 0 || arg2 == 1) {
                    gf_sc.setSelection(2);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        return mView;
    }

    // 时令
    OnClickListener onclick = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            initBgColor();
            switch (arg0.getId()) {
            case R.id.tv_sc:// 蔬菜
                tv_sc.setBackgroundResource(R.drawable.pl_bg);
                break;
            case R.id.tv_sg:// 水果
                tv_sg.setBackgroundResource(R.drawable.pl_bg);
                break;
            case R.id.tv_qt:// 其他
                tv_qt.setBackgroundResource(R.drawable.pl_bg);
                break;
            default:
                break;
            }
        }
    };

    // 初始化背景颜色
    public void initBgColor() {
        tv_qt.setBackgroundResource(R.drawable.pl_bot_input);
        tv_sc.setBackgroundResource(R.drawable.pl_bot_input);
        tv_sg.setBackgroundResource(R.drawable.pl_bot_input);
    }

    // 每日推荐的线程
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                Toast.makeText(mcontext, "点击了评论！", Toast.LENGTH_LONG).show();
                break;
            case 1:
                Toast.makeText(mcontext, "点击了收藏！", Toast.LENGTH_LONG).show();
                break;
            // 查看每日推荐的菜的详细信息
            case 2:
                Intent in = new Intent();
                in.setClass(mcontext, DtlActivity.class);
                mcontext.startActivity(in);
                break;
            default:
                break;
            }
        };
    };

    /**
     * 动态计算滚动条
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}